// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import routers from './router/index'
// eslint-disable-next-line import/no-duplicates
import Router from 'vue-router'
// eslint-disable-next-line import/no-duplicates
import VueRouter from 'vue-router'
import ElementUI from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'
import vuex from 'vuex'
import {store} from './store/store'
Vue.config.productionTip = false
Vue.use(VueRouter)
Vue.use(ElementUI)
Vue.use(vuex)
const originalPush = Router.prototype.push
Router.prototype.push = function push (location, onResolve, onReject) {
  if (onResolve || onReject) return originalPush.call(this, location, onResolve, onReject)
  return originalPush.call(this, location).catch(err => err)
}

const router = new VueRouter({
  mode: 'history',
  routes: routers
})

/* eslint-disable no-new */
new Vue({
  el: '#app',
  router: router,
  store: store,
  components: { App },
  template: '<App/>'
})

router.beforeEach((to, from, next) => {
  // store的getters中定义获取用户信息的函数 getUser
  // userId为空说明用户未登录
  let tem = !!localStorage.getItem('isLogin')
  if (tem !== true) {
    // 跳转到登录页
    if (to.path === '/ShoppingCart' || to.path === '/Information/Profile' || to.path === '/Information/Account' || to.path === '/Information' || to.path === '/Score' || to.path === '/PurchasedItems') {
      alert('请先登录')
      next('/Home')
    } else {
      next()
    }
  } else {
    next()
  }
})
